﻿Imports System.ComponentModel
Imports System.Linq.Expressions

Public Class MyListItem

	Public Property Text As String
	Public Property Value As String

	Public Sub New(ByVal myText As Object, ByVal myValue As Object)
		Text = myText
		Value = myValue
	End Sub

	Public Overrides Function ToString() As String
		Return Text
	End Function
End Class

Public MustInherit Class AutomaticNotfiyPropertyChanged
	Implements INotifyPropertyChanged

	Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

	Protected Sub NotifyPropertyChanged(ByVal Name As String)
		RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Name))
	End Sub

	Protected Sub NotifyPropertyChanged()
		Dim myStackFrame As New StackFrame(1)
		Dim myChangingPropertyNames() As String = myStackFrame.GetMethod.Name.Split("_")
		Dim myChangingPropertyName As String

		If myChangingPropertyNames.Length > 1 Then
			myChangingPropertyName = myChangingPropertyNames(1)
			NotifyPropertyChanged(myChangingPropertyName)
		Else
			'this was likely called as part of a constructor
		End If
	End Sub

	Protected Shared Sub NotifyPropertyChanged(This As AutomaticNotfiyPropertyChanged, Name As String)
		This.NotifyPropertyChanged(Name)
	End Sub

	Protected Overridable Sub NotifyPropertyChanged(Of TResult)(propertyExpr As Expression(Of Func(Of AutomaticNotfiyPropertyChanged, TResult)))
		NotifyPropertyChanged(GetPropertySymbol(propertyExpr))
	End Sub

	Protected Shared Sub NotifyPropertyChanged(Of T, TResult)(This As T, propertyExpr As Expression(Of Func(Of T, TResult)))
		Dim myParent As AutomaticNotfiyPropertyChanged = TryCast(This, AutomaticNotfiyPropertyChanged)

		If myParent IsNot Nothing Then
			AutomaticNotfiyPropertyChanged.NotifyPropertyChanged(DirectCast(myParent, AutomaticNotfiyPropertyChanged), This.GetPropertySymbol(propertyExpr))
		End If
	End Sub

End Class

